//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.aidong.web.controller.pss;

import com.aidong.dao.base.ConditionMap;
import com.aidong.dao.base.OrderSet;
import com.aidong.dao.base.Pagination;
import com.aidong.dao.wills.PssProductStockDao;
import com.aidong.dao.wills.PssTranReceiveDao;
import com.aidong.dao.wills.PssTranSendDao;
import com.aidong.dao.wills.PssTranSendListDao;
import com.aidong.model.wills.Products;
import com.aidong.model.wills.PssPopProduct;
import com.aidong.model.wills.PssTranSend;
import com.aidong.model.wills.PssTranSendImpl;
import com.aidong.model.wills.PssTranSendList;
import com.aidong.service.PssPrintDataService;
import com.aidong.web.controller.pss.PopProductController;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping({"tranSend"})
public class TranSendController extends PopProductController {
    @Resource
    PssTranSendDao pssTranSendDao;
    @Resource
    PssTranSendListDao pssTranSendListDao;
    @Resource
    PssTranReceiveDao pssTranReceiveDao;
    @Resource
    PssProductStockDao pssProductStockDao;
    @Resource
    PssPrintDataService pssprintDataService;

    public TranSendController() {
    }

    @RequestMapping({"list"})
    public ModelAndView list() {
        ModelAndView mv = new ModelAndView("pss/transend/list");
        mv.addObject("shopName", this.getTypeList("shopName"));
        return mv;
    }

    @RequestMapping({"_list"})
    public ModelAndView _list(@RequestParam(value = "sendShop",required = false) String sendShop, @RequestParam(value = "receiveShop",required = false) String receiveShop, @RequestParam(value = "sendDateFrom",required = false) String sendDateFrom, @RequestParam(value = "sendDateTo",required = false) String sendDateTo, @RequestParam(value = "pageNo",required = false) String pageNo, @RequestParam(value = "totalCount",required = false) String totalCount) {
        Pagination pg = new Pagination(1, 20);
        pg.setPage(this.parseInt(pageNo), this.parseInt(totalCount));
        ConditionMap cd = new ConditionMap();
        if(!this.chkNull(sendShop)) {
            cd.eq("sendShop", sendShop);
        }

        if(!this.chkNull(receiveShop)) {
            cd.eq("receiveShop", receiveShop);
        }

        if(!this.chkNull(sendDateFrom)) {
            cd.ge("sendDate", this.parseDate(sendDateFrom, "yyyy-MM-dd"));
        }

        if(!this.chkNull(sendDateTo)) {
            cd.le("sendDate", this.parseDate(sendDateTo + " 23:59:59", "yyyy-MM-dd HH:mm:ss"));
        }

        this.pssTranSendDao.queryPageList(pg, cd, (new OrderSet()).asc("sendID"));
        ModelAndView mv = new ModelAndView("pss/transend/_list");
        mv.addObject("list", pg.getResultList());
        mv.addObject("shopName", this.getTypeList("shopName"));
        mv.addObject("pg", pg);
        return mv;
    }

    @RequestMapping({"add"})
    public ModelAndView add() {
        PssTranSendImpl pssTranSendImpl = new PssTranSendImpl();
        pssTranSendImpl.setQuantity(Double.valueOf(1.0D));
        ModelAndView mv = new ModelAndView("pss/transend/edit");
        mv.addObject("pssTranSend", pssTranSendImpl);
        mv.addObject("shopName", this.getTypeList("shopName"));
        mv.addObject("productUnit", this.getTypeList("productUnit"));
        mv.addObject("_oper", "add");
        return mv;
    }

    @RequestMapping({"_add"})
    @ResponseBody
    public String _add(@ModelAttribute("pssTranSend") PssTranSend pssTranSend, Products products) {
        try {
            pssTranSend.setPrintLabel("N");
            pssTranSend.setCreateuser(this.getLoginUserCode());
            pssTranSend.setCreatetime(new Date());
            pssTranSend.setUpdatetime(new Date());
            PssTranSendList e = null;
            ArrayList lst = new ArrayList();
            Iterator i$ = products.getLstProduct().iterator();

            while(i$.hasNext()) {
                PssPopProduct pc = (PssPopProduct)i$.next();
                e = new PssTranSendList();
                if(!this.chkNull(pc.getProductCode())) {
                    e.setProductCode(pc.getProductCode());
                    e.setQuantity(pc.getQty());
                    lst.add(e);
                }
            }

            this.pssTranSendDao.addTranSend(pssTranSend, this.pssProductStockDao, lst);
            this.addUserLog("新增配送出库的商品", "出库单号:" + pssTranSend.getSendID());
        } catch (Exception var7) {
            this.log.error("add tranSend", var7);
            return this.getResult(false, "新增配送出库的商品发生错误");
        }

        return this.getResult(true, "新增配送出库的商品完成");
    }

    @RequestMapping({"edit"})
    public ModelAndView edit(@RequestParam("sendID") Integer sendID) {
        PssTranSend pssTranSend = (PssTranSend)this.pssTranSendDao.getById(sendID);
        if(pssTranSend != null && pssTranSend.getSendID() != null) {
            if("Y".equals(pssTranSend.getPrintLabel())) {
                return this.showAlert("配送出库的商品已经打印完成，不能修改入库数");
            } else {
                ConditionMap cd = new ConditionMap();
                cd.eq("SendID", sendID);
                List lst = this.pssTranSendListDao.queryTranSendList(cd);
                ModelAndView mv = new ModelAndView("pss/transend/edit");
                mv.addObject("list", lst);
                mv.addObject("pssTranSend", pssTranSend);
                mv.addObject("shopName", this.getTypeList("shopName"));
                mv.addObject("productUnit", this.getTypeList("productUnit"));
                mv.addObject("_oper", "edit");
                return mv;
            }
        } else {
            return this.showAlert("没有这个出库单号的商品");
        }
    }

    @RequestMapping({"_edit"})
    @ResponseBody
    public String _edit(@ModelAttribute("pssTranSend") PssTranSend pssTranSend, Products products) {
        try {
            this.pssTranSendDao.updateTranSend(pssTranSend, this.pssTranSendListDao, this.pssProductStockDao, this.pssTranReceiveDao, products);
            this.reloadTypeList("pssTranSend");
            this.addUserLog("修改配送出库的商品", "出库单号:" + pssTranSend.getSendID());
        } catch (Exception var4) {
            this.log.error("edit tranSend", var4);
            return this.getResult(false, "修改配送出库的商品发生错误");
        }

        return this.getResult(true, "修改配送出库的商品完成");
    }

    @RequestMapping({"print"})
    public ModelAndView print(@RequestParam("sendID") Integer sendID) {
        StringBuilder msg = new StringBuilder();
        String data = this.pssprintDataService.getPssTranData(sendID, msg);
        if(data == null) {
            return this.showAlert(msg.toString());
        } else {
            ModelAndView mv = new ModelAndView("pss/print/print");
            mv.addObject("data", data);
            mv.addObject("title", "配送出库单打印");
            mv.addObject("ptjs", "psstran");
            return mv;
        }
    }
}
